﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace scanner_neb_web
{
    class Program
    {
        static void Main(string[] args)
        {
            //neb有坑，同步不到数据，直接用他们数据中心来获取数据了。
            Console.WriteLine("neb collect v2!");
           
            while (true)
            {
                Console.Write(">");
                var l = Console.ReadLine();
                if (l == "exit")
                {
                    return;
                }
                if (l == "start")
                {
                    Start();
                }
                if (l=="mix")
                {
                    Mix();
                }
            }


        }
        static System.Net.WebClient wc = new System.Net.WebClient();
        async static Task<string> DownState(uint page, uint pagesize)
        {
            try
            {
                string dataapi = "https://data.nebulas.io/holders?page=" + page + "&page_size=" + pagesize;
                var result = await wc.DownloadStringTaskAsync(dataapi);
                return result;
            }
            catch
            {
                return null;
            }
        }
        static async void Start()
        {

            uint step = 100;
            uint page = GetCurCount() / step + 1;


            while (page < 346)
            {
                var result = await DownState(page, step);
                if (result == null)
                {
                    Console.WriteLine("星云链土豆服务器挂啦");
                    continue;
                }
                var filename = "state_" + page + ".json";
                Console.WriteLine("write " + filename);
                System.IO.File.WriteAllText(filename, result);
                SetCurCount(step * page);
                page++;
            }
        }
        static async void Mix()
        {
            var files = System.IO.Directory.GetFiles(".", "state_*.json");
            Console.WriteLine("haveline:" + files.Length);
            List<string> lists = new List<string>();
            foreach(var f in files)
            {
                var json = Newtonsoft.Json.Linq.JObject.Parse(System.IO.File.ReadAllText(f));
                var arr = json["data"]["list"] as Newtonsoft.Json.Linq.JArray;
                foreach(var item in arr)
                {
                    var addr=                  item["address"];
                    lists.Add(addr.ToString());
                }
            }
            System.IO.File.WriteAllLines("neball.txt", lists.ToArray());
                
        }
        static uint GetCurCount()
        {
            if (System.IO.File.Exists("count.txt"))
            {
                var count = uint.Parse(System.IO.File.ReadAllText("count.txt"), System.Globalization.NumberStyles.HexNumber);
                return count;
            }
            return 0;
        }
        static void SetCurCount(uint count)
        {
            System.IO.File.WriteAllText("count.txt", count.ToString("X08"));
        }

    }
}
